<?php

namespace app\supplier\model\stall;

use app\common\model\product\Product as ProductModel;
use app\common\model\product\Category as CategoryModel;
use app\common\model\supplier\CategoryStatus as CategoryStatusModel;
use app\common\model\stall\Category as StallCategoryModel;

/**
 * 商品分类模型
 */
class Category extends CategoryModel
{


    public function getCategorytree($stall_id, $shop_supplier_id = 0)
    {
        $SupplierClist = $this->getSupplierCate($shop_supplier_id)['tree'];
        $this->getStallList($stall_id, $SupplierClist);
        return $SupplierClist;
    }

    public function getStallCateTree($stall_id, $shop_supplier_id = 0){
        $SupplierClist = $this->getSupplierCate($shop_supplier_id)['tree'];
        $this->getStallListShow($stall_id, $SupplierClist);
        return $SupplierClist;
    }

    private function getStallList($stall_id, &$SupplierClist)
    {
        foreach ($SupplierClist as &$v) {
            $v['show'] = self::getStallStatus($stall_id, $v['category_id']) ? 1 : 0;
            if (isset($v['child']) && count($v['child']) > 0) {
                $this->getStallList($stall_id, $v['child']);
            }
        }
    }

    private function getStallListShow($stall_id, &$SupplierClist)
    {
        foreach ($SupplierClist as $key=>&$v) {
            if(!self::getStallStatus($stall_id, $v['category_id'])){
                unset($SupplierClist[$key]);
                continue;
            }
            if (isset($v['child']) && count($v['child']) > 0) {
                $this->getStallListShow($stall_id, $v['child']);
            }
        }
        $SupplierClist = array_values($SupplierClist);
    }

    private static function getStallStatus($stall_id, $cateid = 0): bool
    {
        if (StallCategoryModel::checkDB($stall_id, $cateid)) {
            return false;
        }
        return true;
    }

    /**
     * @Exp 获取商户展示分类
     * @Auth Mr.Z
     * @Date 2024/7/9
     * @param $shop_supplier_id 商户id
     * @return array
     */
    private static function getSupplierCate($shop_supplier_id = 0)
    {
        $Cmodel = (new CategoryStatusModel())->alias('s')->where('s.shop_supplier_id', '=', $shop_supplier_id)->join('category c', 'c.category_id=s.category_id');
        $Cmodel_b = $Cmodel;
        $data = $Cmodel_b->where('s.show_status', '=', 1)
            ->field('s.category_id as category_id,c.name as name,s.show_status as is_show,s.sort as sort,c.parent_id as parent_id,c.type as type,c.image_id as image_id')
            ->order('sort asc')
            ->select();

        $all = !empty($data) ? $data->toArray() : [];

        $tree = [];
        foreach ($all as $first) {
            if ($first['parent_id'] != 0) continue;
            $twoTree = [];
            foreach ($all as $two) {
                if ($two['parent_id'] != $first['category_id']) continue;
                $threeTree = [];
                foreach ($all as $three)
                    $three['parent_id'] == $two['category_id']
                    && $threeTree[$three['category_id']] = $three;
                !empty($threeTree) && $two['child'] = $threeTree;
                array_push($twoTree, $two);
            }
            if (!empty($twoTree)) {
                $temp_two_tree = array_column($twoTree, 'sort');
                array_multisort($temp_two_tree, SORT_ASC, $twoTree);
                $first['child'] = $twoTree;
            }
            array_push($tree, $first);
        }

        return compact('all', 'tree');
    }




    /**
     * @Exp    编辑状态
     * @Author Mr.Z
     * @Date   2024-05-11
     * @param  [type]     $postData         [description]
     * @param integer $shop_supplier_id [description]
     */
    public function setStatus($postData, $stall_id)
    {
        $formlist = $postData['form'];
        $oknum = 0;
        if ($formlist) {
            foreach ($formlist as $value) {
                $category_id = $value['category_id'];
                //检查是否存在
                $saveData = [
                    'category_id' => $category_id,
                    'stall_id' => $stall_id,
                    'create_time' => time(),
                    'update_time' => time(),
                    'app_id' => self::$app_id
                ];
                $Strs = StallCategoryModel::checkDB($stall_id, $category_id, true);
                if (StallCategoryModel::updateShowStatus($Strs['id'] ?? 0, $value['show'], $saveData)) {
                    $oknum++;
                }
            }
        }
        if ($oknum > 0) {
            return true;
        }
        return false;
    }

    /**
     * @Exp    重组数据
     * @Author Mr.Z
     * @Date   2024-05-11
     * @param  [type]     $data             [description]
     * @param integer $shop_supplier_id [description]
     * @param integer $type [description]
     */
    public function setData($data, $shop_supplier_id = 0, $type = 0)
    {
        $savedata = [];
        $savedata['show_status'] = $data['is_show'];
        $savedata['sort'] = $data['sort'];
        $savedata['update_time'] = time();
        if ($type == 1) {
            $savedata['category_id'] = $data['category_id'];
            $savedata['shop_supplier_id'] = $shop_supplier_id;
            $savedata['create_time'] = time();
            $savedata['app_id'] = self::$app_id;
        }
        return $savedata;
    }
}
